Attribute VB_Name = "Cal"
Public StateNum As Integer
Public TimeGapFlag As Boolean
Public reTestNum As Integer
Public stateName() As String
Public CalKit() As Integer
Dim MaxChNum As Integer
Public pos As Integer 'is a count for state number
Dim CalKits As Integer
Dim ch As Integer
Public CalPath As String
Public flag As Integer 'used to dectect whether enable done button
'add at 2012.2.16 by zhangxianfeng
Public CalTestArr(1 To 4, 1 To 4)
Public CalSXY As Double
Public CalSZZ As Double
Public Function CalIni()
          CalPath = stateName(StateNum - pos)
          CalKits = CalKit(StateNum - pos)
          SCPI.MMEMory.Load.state = CalPath
          SCPI.DISPlay.Split = "D12_34"
          MaxChNum = SCPI.SERVice.CHANnel.count
          For ch = 1 To MaxChNum
              SCPI.SENSe(ch).CORRection.COLLect.CKIT.Select = CalKits
              dmy = SCPI.IEEE4882.OPC
          Next ch
          'MsgBox StateNum - pos
End Function
Public Function Cal_Step_1()
    'MsgBox "Connect OPEN to Port 1"
For ch = 1 To MaxChNum
    SCPI.SENSe(ch).CORRection.COLLect.METHod.SOLT2 = Array(1, 2)
    SCPI.SENSe(ch).CORRection.COLLect.ACQuire.Open = 1
    dmy = SCPI.IEEE4882.OPC
Next ch

End Function
Public Function Cal_Step_2()
    'MsgBox "Connect Short to Port 1"
    For ch = 1 To MaxChNum
    SCPI.SENSe(ch).CORRection.COLLect.ACQuire.SHORt = 1
    dmy = SCPI.IEEE4882.OPC
    Next ch
End Function
Public Function Cal_Step_3()
    'MsgBox "Connect Load to Port 1"
    For ch = 1 To MaxChNum
    SCPI.SENSe(ch).CORRection.COLLect.ACQuire.Load = 1
    dmy = SCPI.IEEE4882.OPC
    Next ch
End Function
Public Function Cal_Step_4()
    'MsgBox "Connect OPEN to Port 2"
    For ch = 1 To MaxChNum
    SCPI.SENSe(ch).CORRection.COLLect.ACQuire.Open = 2
    dmy = SCPI.IEEE4882.OPC
    Next ch
End Function
Public Function Cal_Step_5()
   'MsgBox "Connect Short to Port 2"
   For ch = 1 To MaxChNum
    SCPI.SENSe(ch).CORRection.COLLect.ACQuire.SHORt = 2
    dmy = SCPI.IEEE4882.OPC
   Next ch
End Function
Public Function Cal_Step_6()
    'MsgBox "Connect Load to Port 2"
    For ch = 1 To MaxChNum
    SCPI.SENSe(ch).CORRection.COLLect.ACQuire.Load = 2
    dmy = SCPI.IEEE4882.OPC
    Next ch
End Function
Public Function Cal_Step_7()
   ' MsgBox "Connect Port1 to Port2"
    For ch = 1 To MaxChNum
    SCPI.SENSe(ch).CORRection.COLLect.ACQuire.THRU = Array(2, 1)
    dmy = SCPI.IEEE4882.OPC
    Next ch
    For ch = 1 To MaxChNum
    SCPI.SENSe(ch).CORRection.COLLect.ACQuire.THRU = Array(1, 2)
    dmy = SCPI.IEEE4882.OPC
Next ch
End Function
Public Function Cal_Step_8()
For ch = 1 To MaxChNum
    SCPI.SENSe(ch).CORRection.COLLect.SAVE
    dmy = SCPI.IEEE4882.OPC
Next ch
SCPI.MMEMory.STORe.STYPe = "CST"
SCPI.MMEMory.STORe.state = CalPath
dmy = SCPI.IEEE4882.OPC
End Function
'Public Function Check_Cal() As String
'       Dim Modify_Time() As Date
'       ReDim Modify_Time(StateNum - 1) As Date
'       For i = 0 To StateNum - 1
'            Modify_Time(i) = FileDateTime(StateName(i))
'       Next i
'       Dim Date_Now As Date
'       Date_Now = Date
'       Dim Flag_pos() As Integer
'       ReDim Flag_pos(0)
'       Dim counter As Integer
'       Dim counter_origin As Integer
'       counter = 0
'       For i = 0 To StateNum - 1
'           If Date_Now - Modify_Time(i) >= 6 Then
'              counter_origin = counter_origin + 1
'              Flag_pos(counter) = i
'              If i <> StateNum - 1 Then
'              counter = counter + 1
'              ReDim Preserve Flag_pos(counter)
'              End If
'           End If
'       Next i
'       Erase Modify_Time
'       Dim str As String
'       For i = 0 To counter
'            str = str & Flag_pos(i) + 1 & ","
'       Next i
'       Erase Flag_pos
'       If counter_origin = 0 Then
'          MsgBox "No need to calitrate"
'       Else
'          MsgBox "The " & str & " state file(s) should be  calibrated!"
'       End If
'End Function
Function CalProc()
    StateNum = 1
    ReDim stateName(StateNum)
    For i = 0 To ItemNum - 1
        For j = 0 To StateNum - 1
            If DevConf(i).Sta = stateName(j) Then
                Exit For
            Else
                pos = pos + 1
            End If
        Next j
        If pos = StateNum Then
           stateName(StateNum - 1) = DevConf(i).Sta
           StateNum = StateNum + 1
           ReDim Preserve stateName(StateNum)
        End If
        pos = 0
    Next i
End Function

Function FixedDrow(fixName As String) As Double

    Dim tempString() As String
    tempString = Split(fixName, "#")
    FixedDrow = CDbl(tempString(UBound(tempString)))
    
End Function

'add at 2012.2.16 by zhangxianfeng
Function Cal_test(meastrace As Integer) As Variant
On Error GoTo exitcode
ChNum = SCPI.SERVice.CHANnel.count
If meastrace = 3 Then
    For ch = 1 To ChNum
        SingleSweep (ch)
        SCPI.CALCulate(ch).PARameter(meastrace).Select
        SCPI.CALCulate(ch).SELected.MARKer(1).state = True
        Start = SCPI.SENSe(ch).FREQuency.Start
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.DOMain.Start = Start
        stp = SCPI.SENSe(ch).FREQuency.STOP
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.DOMain.STOP = stp
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.DOMain.state = True
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.Type = "MAX"
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.EXECute
        temp = SCPI.IEEE4882.OPC
        maxdata = Val(SCPI.CALCulate(ch).SELected.MARKer(1).y(0))
        
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.Type = "MIN"
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.EXECute
        temp = SCPI.IEEE4882.OPC
        mindata = Val(SCPI.CALCulate(ch).SELected.MARKer(1).y(0))
        CalTestArr(ch, meastrace) = Val(Format(IIf(Abs(maxdata) >= Abs(mindata), maxdata, mindata), "##.###"))
    Next ch
End If
If meastrace = 1 Or meastrace = 2 Then
    For ch = 1 To ChNum
        SingleSweep (ch)
        SCPI.CALCulate(ch).PARameter(meastrace).Select
        SCPI.CALCulate(ch).SELected.MARKer(1).state = True
        Start = SCPI.SENSe(ch).FREQuency.Start
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.DOMain.Start = Start
        stp = SCPI.SENSe(ch).FREQuency.STOP
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.DOMain.STOP = stp
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.DOMain.state = True
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.Type = "MAX"
        SCPI.CALCulate(ch).SELected.MARKer(1).Function.EXECute
        temp = SCPI.IEEE4882.OPC
        CalTestArr(ch, meastrace) = Val(Format(SCPI.CALCulate(ch).SELected.MARKer(1).y(0), "##.##"))
    Next ch
End If
exitcode:
End Function

Sub IniFileCope(pathName As String, fileName As String)
    
    Dim dPath As String
    Dim myTempArray() As String
    myTempArray = Split(fileName, "\")
    dPath = "D:\" + myTempArray(UBound(myTempArray)) + ".ini"
    
    FileCopy pathName, dPath
    pathName = dPath
    
End Sub

Function GetID(stateName As String) As Integer

    Dim u As Integer
    Dim i As Integer
    GetID = -1
    u = UBound(DevConf)
    For i = 0 To u
        If (DevConf(i).Sta = stateName) Then
            GetID = i
            Exit For
        End If
    Next i

End Function
